<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Article extends Model
{
    protected $guarded=['id','created_at','updated_at'];
    protected $casts=[
        'recommend'=>'boolean',
        'show_status'=>'boolean',
        'essence'=>'boolean',
    ];

    /**
     * 文章所属分类
     */
    public function cate()
    {
        return $this->belongsTo(Cate::class,'cate_id');
    }

    /**
     * 图片访问修改器
     */
    public function getThumbAttribute($value)
    {
        $path=env('APP_URL').'/uploads/';

        if($value){
            return $path.$value;
        }else{
            if(isset($this->cate->thumb))return $path.$this->cate->thumb;
        }
        return '';
    }

    /**
     * 时间访问器
     * @param $value
     */
    public function getCreatedAtAttribute($value)
    {
        return date('Y/m/d',strtotime($value));
    }

    /**
     * 获取内容
     */
    public function getContentAttribute($value)
    {
        if(!$this->content_file) return $value;
        //优先获取文件数据
        $filePath=public_path().'/'.trim($this->content_file,'/');

        if (!file_exists($filePath)) return $value;

        return file_get_contents($filePath);

    }


}
